/**
 * 
 */
package org.statewalker.core.server;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.statewalker.core.IEvent;
import org.statewalker.core.IState;
import org.statewalker.core.IStateExecutor;
import org.statewalker.core.Process;
import org.statewalker.core.IState.ICallback;

public class ThreadedStateExecutor implements IStateExecutor {

    private Executor fExecutor;

    public ThreadedStateExecutor() {
        this(Executors.newSingleThreadExecutor());
    }

    public ThreadedStateExecutor(Executor defaultExecutor) {
        fExecutor = defaultExecutor;
    }

    public ThreadedStateExecutor(int threadPoolSize) {
        this(Executors.newFixedThreadPool(threadPoolSize));
    }

    public void activateState(
        Process process,
        final IState state,
        final IEvent event) {
        final ICallback callback = process.getStateCallback();
        fExecutor.execute(new Runnable() {
            public void run() {
                state.activate(event, callback);
            }
        });
    }

    public void deactivateState(
        Process process,
        final IState state,
        final IEvent event) {
        final ICallback callback = process.getStateCallback();
        fExecutor.execute(new Runnable() {
            public void run() {
                state.deactivate(event, callback);
            }
        });
    }

    public Executor getExecutor() {
        return fExecutor;
    }

    public void setExecutor(Executor executor) {
        fExecutor = executor;
    }

}